import 'package:flutter/material.dart';
import 'package:fzcgx_app/common/adapter_utils.dart';
import 'package:fzcgx_app/common/assets_utils.dart';
import 'package:fzcgx_app/provider/provider_widget.dart';
import 'package:fzcgx_app/ui/base/base_view_model.dart';
import 'package:fzcgx_app/ui/my/address/page/address_add_page.dart';
import 'package:fzcgx_app/ui/my/address/page/address_edit_page.dart';
import 'package:fzcgx_app/ui/my/address/view_model/address_view_model.dart';
import 'package:fzcgx_app/ui/widget/custom_appbar_widget.dart';
import 'package:nav_router/nav_router.dart';

class AddressPage extends StatefulWidget {
      AddressPage({Key key,this.callBack}) : super(key: key);
final Function callBack;
  @override
  _AddressPageState createState() => _AddressPageState();
}

class _AddressPageState extends State<AddressPage> {
  AddressViewModel viewModel;
  @override
  void initState() {
    viewModel = AddressViewModel();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0xff141F33),
      appBar: CustomAppbar(
        backgroundColor: Color(0xff141F33),
        title: '收货地址',
        themeColor: Colors.white,
        onTap: (){
           Navigator.pop(context);
           if(widget.callBack!=null){
              widget.callBack();
           }
        },
        actions: <Widget>[
          InkWell(
              onTap: () {
                routePush(AddressAddPage(
                  callBack: () {
                    viewModel.requestApi();
                  },
                )).then((value) {
                  viewModel.requestApi();
                });
                
              },
              child: Container(
                alignment: Alignment.center,
                padding: EdgeInsets.only(right: AdapterUtils.getWidth(15)),
                child: Text('新增地址',
                    style: TextStyle(
                        color: Color(0xff99A2AD),
                        fontWeight: FontWeight.w500,
                        fontSize: AdapterUtils.getSp(14))),
              ))
        ],
      ),
      body: SafeArea(
          child: ProviderWidget(
        model: viewModel,
        onReady: (viewModel) {
          viewModel.requestApi();
        },
        builder: (context, viewModel, child) {
          return viewModel.state == ViewState.Success
              ? _buildSuccessWidget(viewModel)
              : Container();
        },
      )),
    );
  }

  Widget _buildSuccessWidget(AddressViewModel viewModel) {
    return SingleChildScrollView(
      child: Column(
        children: <Widget>[
          SizedBox(height: AdapterUtils.getHeight(15)),
          ListView.builder(
              shrinkWrap: true,
              primary: false,
              itemCount: viewModel.models.length,
              itemBuilder: (BuildContext context, int index) {
                return Container(
                    padding: EdgeInsets.symmetric(
                        horizontal: AdapterUtils.getWidth(15)),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Row(
                          children: <Widget>[
                            Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: <Widget>[
                                Text(
                                    '${viewModel.models[index].acceptName} ${viewModel.models[index].phone}',
                                    style: TextStyle(
                                        color: Colors.white,
                                        fontWeight: FontWeight.bold,
                                        fontSize: AdapterUtils.getSp(14))),
                                SizedBox(
                                  height: AdapterUtils.getHeight(6),
                                ),
                                Row(
                                  children: <Widget>[
                                    viewModel.models[index].check == 1
                                        ? Container(
                                            alignment: Alignment.center,
                                            width: AdapterUtils.getWidth(28),
                                            height: AdapterUtils.getHeight(18),
                                            decoration: BoxDecoration(
                                                border: Border.all(
                                                    color: Color(0xffFF3D3E),
                                                    width: 1),
                                                borderRadius: BorderRadius.all(
                                                    Radius.circular(4))),
                                            child: Text('默认',
                                                textAlign: TextAlign.center,
                                                style: TextStyle(
                                                    color: Color(0xffFF3D3E),
                                                    fontWeight: FontWeight.bold,
                                                    fontSize:
                                                        AdapterUtils.getSp(
                                                            10))),
                                          )
                                        : Container(),
                                    viewModel.models[index].check == 1
                                        ? SizedBox(
                                            width: AdapterUtils.getWidth(7))
                                        : Container(),
                                    Text(
                                        '${viewModel.models[index].cityName} ${viewModel.models[index].address}',
                                        style: TextStyle(
                                            color: Colors.white,
                                            fontWeight: FontWeight.w500,
                                            fontSize: AdapterUtils.getSp(14))),
                                  ],
                                )
                              ],
                            ),
                            Spacer(),
                            GestureDetector(
                              onTap: (){
                                routePush(AddressEditPage(model:viewModel.models[index],callBack: (){
                                  viewModel.requestApi();
                                },));
                                
                              },
                              child: Container(
                                width: AdapterUtils.getWidth(50),
                                height: AdapterUtils.getHeight(100),
                                alignment: Alignment.centerRight,
                                child: Image.asset(
                                AssetsUtils.getAssetsImagesPath('edit.png'),),
                              )
                            )
                          ],
                        ),
                        
                      ],
                    ));
              })
        ],
      ),
    );
  }
}
